﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 标准字典管理
    /// 
    /// </summary>
    public class StdDictService : DcAppService
    {
        private readonly IRepository<StdWordDict, Guid> _stdWordDictRepository;
        private readonly IRepository<StdDomainDict, Guid> _stdDomainDictRepository;
        private readonly IRepository<StdPhrDict, Guid> _stdPhrDictRepository;

        public StdDictService(IRepository<StdWordDict, Guid> standardWordDictionaryRepository
            , IRepository<StdDomainDict, Guid> domainDictionaryRepository
            , IRepository<StdPhrDict, Guid> stdPhrDictRepository)
        {
            _stdWordDictRepository = standardWordDictionaryRepository;
            _stdDomainDictRepository = domainDictionaryRepository;
            _stdPhrDictRepository = stdPhrDictRepository;
        }

        #region 标准域

        /// <summary>
        /// 获取标准域
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<StdDomainDictDto>> GetStdDomainListAsync(StdWordDictInput input)
        {
            var iQuery = await _stdDomainDictRepository.GetQueryableAsync();

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StdDomainDict>, List<StdDomainDictDto>>(iQuery.ToList());

                return new PagedResultDto<StdDomainDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<StdDomainDict>, List<StdDomainDictDto>>(items.ToList());

                return new PagedResultDto<StdDomainDictDto>(totalCount, itemsDto);
            }
        }

        #endregion

        #region 标准用语

        public async Task<PagedResultDto<StdPhrDictDto>> GetStdPhrListAsync(StdWordDictInput input)
        {
            var iQuery = await _stdPhrDictRepository.GetQueryableAsync();

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<StdPhrDict>, List<StdPhrDictDto>>(iQuery.ToList());

                return new PagedResultDto<StdPhrDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<StdPhrDict>, List<StdPhrDictDto>>(items.ToList());

                return new PagedResultDto<StdPhrDictDto>(totalCount, itemsDto);
            }
        }

        #endregion
    }
}
